package com.gzsxy.esjy.service.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.esjy.bus.log.annotation.OperLog;
import com.gzsxy.esjy.bus.log.enum1.LogActionType;
import com.gzsxy.esjy.service.base.dto.request.CategoryRequest;
import com.gzsxy.esjy.service.base.dto.response.CategoryRsp;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;
import com.gzsxy.esjy.service.base.dto.response.PageResult;
import com.gzsxy.esjy.service.product.entity.vo.CategoryQueryVo;
import com.gzsxy.esjy.service.product.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 * 商品类型 前端控制器
 * </p>
 *
 * @author zxl
 * @since 2021-11-02
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/admin/product/category")
@Api("商品分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    @ApiOperation("测试配置中心刷新")
    public String getConfigInfo() {
        return configInfo;
    }




    @GetMapping("/list")
    @ApiOperation("分页查询商品分类列表")
    public DefaultResponse getAllCategory(@ApiParam(value = "当前页面",required = false,defaultValue ="1" ) @RequestParam(required = false) Long page,         //long数据类型适用于较大整数类型
                                          @ApiParam(value = "每页记录数",required = false,defaultValue ="5") @RequestParam(required = false) Long limit,
                                          @ApiParam("查询对象") CategoryQueryVo categoryQueryVo){
        IPage<?> pageParam = null;
        //分页查询
        if (page !=null && limit !=null){
            pageParam = new Page<>(page, limit);
        }
        PageResult<CategoryRsp> rspIPage = categoryService.selectPage(pageParam,categoryQueryVo);
        return DefaultResponse.ok(rspIPage);
    }



    @PostMapping("/add")
    @ApiOperation("添加分类")
    @OperLog(operModul = "商品分类管理",operType = LogActionType.ADD,operDesc = "添加分类")
    public DefaultResponse addCategory(@RequestBody CategoryRequest categoryRequest){
        categoryService.addCategory(categoryRequest);
        return DefaultResponse.ok();
    }

    @PutMapping("/update")
    @ApiOperation("更新分类")
    @OperLog(operModul = "商品分类管理",operType = LogActionType.UPDATE,operDesc = "更新分类")
    public DefaultResponse updateCategory(@RequestBody CategoryRequest categoryRequest){
        categoryService.updateCategory(categoryRequest);
        return DefaultResponse.ok();
    }


    @DeleteMapping("/delete/{categoryId}")
    @ApiOperation("删除分类")
    @OperLog(operModul = "商品分类管理",operType = LogActionType.DELETE,operDesc = "删除分类")
    public DefaultResponse deleteCategory(@ApiParam(value = "分类id",required = true) @PathVariable String categoryId,
                                          @ApiParam(value = "是否确认操作",required = true) @RequestParam Boolean isConform){
        categoryService.deleteCategory(categoryId,isConform);
        return DefaultResponse.ok();
    }



}

